<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright"  />
<title></title>
<link rel="stylesheet" type="text/css" href="../easyui/1.5.3/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../css/wu.css" />
<link rel="stylesheet" type="text/css" href="../css/icon.css" />
<script type="text/javascript" src="../js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
<script type="text/javascript" src="../easyui/1.5.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../easyui/1.5.3/locale/easyui-lang-zh_CN.js"></script>
<!--<script type="text/javascript" src="../js/xheditor/jquery/jquery-1.4.4.min.js"></script>-->
<script type="text/javascript" src="../js/xheditor/xheditor-1.2.2.min.js"></script>
<script type="text/javascript" src="../js/xheditor/xheditor_lang/zh-cn.js"></script>
<script type="text/javascript" src="../js/jquery.parseurl.min.js"></script>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'center',title:'News Manager',iconCls:'icon-ok'" style="height:auto">
			<div id="comment-toolbar">
			    <div class="wu-toolbar-button">
			        <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="openCommentAdd()" plain="true">添加</a>
			        <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="" plain="true">修改</a>
			        <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="removeNews()" plain="true">删除</a>
			        <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="reload()" plain="true">刷新</a>
			        
			    </div>
			    <div class="wu-toolbar-search">
			        <label>起始时间：</label><input class="easyui-datebox" style="width:100px">
			        <label>结束时间：</label><input class="easyui-datebox" style="width:100px">
			        <label>关键词：</label><input class="wu-text" style="width:100px">
			        <a href="#" class="easyui-linkbutton" iconCls="icon-search">开始检索</a>
			    </div>
			</div>
			
			<table id="dg"></table>
			
			
			
			<div id="comment-dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width:80%; padding:10px;">
				 <form id="comment-form" class="easyui-form" method="post" data-options="novalidate:true">
				   <div style="margin-bottom:20px">
				       	<textarea class="xheditor" name="comment" id="comment" style="width:100%;height:150px" data-options="required:true"></textarea>
				    </div>
				    <div style="margin-bottom:20px;display:block">
				       	<input class="easyui-textbox" name="cateId" id="cateId" style="width:100%" data-options="label:'cateId',required:true">
				    </div>
				</form>
			</div>
			
		</div>
		
	</div>
	
    <script type="text/javascript">
    	var newsId=$.parseUrl(window.location.href).query.id;
    	var globalId = null;
    	$(pageInit);
        function pageInit()
        {
            $('#content').xheditor({
                html5Upload:false,
                upMultiple:1,

                upLinkUrl:"upload.php",
                upLinkExt:"zip,rar,txt",

                upImgUrl:"/comment/upLoadImg",
                upImgExt:"jpg,jpeg,gif,png",

                upFlashUrl:"upload.php",
                upFlashExt:"swf",

                upMediaUrl:"upload.php",
                upMediaExt:"wmv,avi,wma,mp3,mid"
            });
           
        }
    	
		
		function initList(cateId){
    		
    		$('#cateId').val(cateId);
    		if(cateId!=undefined && cateId.trim().length>0){
    			queryParams = {cateId:cateId}
    			console.log(cateId);
    		}else{
    			queryParams = {}
    		}
    		
    		$('#dg').datagrid({
			    url:'http://localhost:3000/comment/list',
			    method:"post",
			    fit:true,
			    queryParams:queryParams,
			    pageSize:4,
			    pageList:[2,4,6,8,10],
			    pagination:true,
			    toolbar:'#comment-toolbar',
			    columns:[[
			    	{ checkbox:true,field:'columns'},
			        {field:'comment',title:'评论',width:550},
			        {field:'date',title:'发布时间',width:200},
			        {field:'operate',title:'管理', width:180,
						formatter: function(value,row,index){
							return '<a  href="javascript:void(0)"  onclick="openCommentEdit(\''+ row._id+'\')" class="easyui-linkbutton change">修改</a> '+
							'<a href="javascript:void(0)" onclick="deleComment(\''+ row._id+'\')" class="easyui-linkbutton del">删除</a>'
						}
					}
			    ]],
			    onLoadSuccess:function(data){  
			        $('.change').linkbutton({text:'修改',plain:true,iconCls:'icon-edit'}); 
			        $('.del').linkbutton({text:'删除',plain:true,iconCls:'icon-edit'});
			    }  
			});
			/*$('#dg').datagrid('reload');*/
			
    	}
    	initList(newsId);
    	
    	
    	//添加
    	function openCommentAdd(){
			$("#comment-dialog").dialog({
				closed: false,
				modal:true,
	            title: "添加评论",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
	                handler: commentAddAction,
	                
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#comment-dialog').dialog('close');                    
	                }
	            }]
	        });
		}
		
		
		function commentAddAction(){
			 $('#comment-form').form('submit',{
		        onSubmit:function(){
		        	
		            if ($(this).form('enableValidation').form('validate')){
		            	console.log($('#comment-form').serializeJSON());
						$.ajax({
							type:"post",
							url:"http://localhost:3000/comment/data",
							data:$("#comment-form").serializeJSON()
						}).then(function(res){
							$('#dg').datagrid('reload');
						});
						$('#comment').val('');
						$('#comment-dialog').dialog('close');
		            }else{
		            	alert('no')
		            }
		        }
		    });
		}
    	
		//编辑
		function openCommentEdit(id){
			
			globalId = id;
			alert(id);
			$('#cateId').textbox('setValue',newsId);
			$.ajax({
				type:"post",
				url:"http://localhost:3000/comment/data/"+id,
				data:{}
			}).then(function(res){
				$('#comment').val(res.comment);
				
			})
			
			$("#comment-dialog").dialog({
				closed: false,
				modal:true,
	            title: "修改评论",
	            buttons: [{
	                text: '确定',
	                iconCls: 'icon-ok',
					handler: editComment,
	            }, {
	                text: '取消',
	                iconCls: 'icon-cancel',
	                handler: function () {
	                    $('#comment-dialog').dialog('close');                    
	                }
	            }]
	        });
		}
		
		function editComment(){

			
			$('#comment-form').form('submit',{
		        onSubmit:function(){
		            if ($(this).form('enableValidation').form('validate')){
		            	console.log($('#comment-form').serializeJSON());
		            	alert(globalId);
						$.ajax({
							type:"post",
							url:"http://localhost:3000/comment/data/"+globalId,
							data:$("#comment-form").serializeJSON()
						}).then(function(res){
							$('#dg').datagrid('reload');
						});
						$('#comment').val('');

						$('#comment-dialog').dialog('close');
		            }else{
		            	alert('no')
		            }
		        }
		    });

		}
		
		function deleComment(id){
			$.ajax({
				type:"delete",
				url:"http://localhost:3000/comment/data/"+ id
			}).then(function(res){		
				$('#dg').datagrid('reload');
			});
		}
		
	</script>
</body>
</html>
